import time
from django.core.paginator import Paginator
from django.shortcuts import render, redirect
from apps.systemctl import auth, menu, vm, host_machine_ret
from .models import Backup


@auth
def list_backup(request):
    error_msg = ""
    menus = menu(request)

    user = request.GET.get('user')
    list_str = request.GET.get('list')
    backup_ret = Backup.objects.all().order_by("apply_date")
    page = Paginator(backup_ret.order_by('name'), 10)
    page_id = request.GET.get('page_id')
    if page_id is None:
        page_id = 1
    stus = page.page(int(page_id))
    return render(request, 'backup/backup_list.html',
                  {'ret': stus,
                   'error_msg': error_msg,
                   'pageid': int(page_id),
                   'page': page,
                   'menu': menus,
                   'list': int(list_str),
                   'user': user})


@auth
def add_backup(request):
    error_msg = ""
    host_machine_all = host_machine_ret(request)

    menus = menu(request)
    user = request.GET.get('user')
    list_str = request.GET.get('list')

    return render(request, 'backup/backup_add.html',
                  {'error_msg': error_msg,
                   'menu': menus,
                   'list': int(list_str),
                   'user': user})


@auth
def edit_backup(request):
    error_msg = ""
    host_machine_all = host_machine_ret(request)

    menus = menu(request)
    user = request.GET.get('user')
    list_str = request.GET.get('list')

    return render(request, 'backup/backup_edit.html',
                  {'error_msg': error_msg,
                   'menu': menus,
                   'list': int(list_str),
                   'user': user})


@auth
def delete_backup(request):
    error_msg = ""

    menus = menu(request)
    user = request.GET.get('user')
    list_str = request.GET.get('list')

    return render(request, 'backup/backup_delete.html',
                  {'error_msg': error_msg,
                   'menu': menus,
                   'list': int(list_str),
                   'user': user})
